package com.cdivtc.management.service;

import com.cdivtc.management.dao.ClazzMapper;
import com.cdivtc.management.dao.CollageMapper;
import com.cdivtc.management.dao.MajorMapper;
import com.cdivtc.management.pojo.Clazz;
import com.cdivtc.management.pojo.Collage;
import com.cdivtc.management.pojo.Major;
import com.cdivtc.management.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class ClazzAndCollage {

    @Autowired
    private ClazzMapper clazzMapper;
    @Autowired
    private CollageMapper collageMapper;
    @Autowired
    private MajorMapper majorMapper;
    @Autowired
    private IdWorker idWorker;

    public List<Major> queryAllClazz(String colid) {
        Major major = new Major();
        major.setCollageid(colid);
        List<Major> select = majorMapper.select(major);
        if (select == null || select.size() == 0) {
            throw new IllegalArgumentException("没有查询到数据");
        }
        return select;
    }

    public List<Collage> queryAllCollage() {
        return collageMapper.selectAll();
    }

    public List<Clazz> queryBymajorId(String majorid) {
        Clazz clazz = new Clazz();
        clazz.setMajorname(majorid);
        List<Clazz> select = clazzMapper.select(clazz);
        if (select == null || select.size() == 0) {
            throw new IllegalArgumentException("没有查询到数据");
        }
        return select;
    }

    public List<Clazz> queryByMajorAndClazzAll() {
        Example example = new Example(Major.class);
        example.setOrderByClause("majorname desc");

        List<Major> majors = majorMapper.selectAll();
        List<Clazz> clazzes = clazzMapper.selectByExample(example);
        for (int i = 0; i < clazzes.size(); i++) {
            for (Major major : majors) {
                if (clazzes.get(i).getMajorname().equals(major.getId())) {
                    clazzes.get(i).setClazzName(clazzes.get(i).getClazzName()+major.getMajorname());
                }
            }
        }
        return clazzes;
    }

    //    根据班级查 专业  学院
    public Clazz queryMajorAndCollageByClazzId(String clazzId){
        Clazz clazz = clazzMapper.selectByPrimaryKey(clazzId);
        ;

        Major major = majorMapper.selectByPrimaryKey(clazz.getMajorname());
        Collage collage = collageMapper.selectByPrimaryKey(clazz.getColId());
        clazz.setMajorname(major.getMajorname());
        clazz.setColId(collage.getColname());
        return clazz;
    }

    public void addCollege(Collage collage) {
      Example example = new Example(Collage.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("colname",collage.getColname());
        Collage collage1 = collageMapper.selectOneByExample(example);
        System.out.println("collage1 = " + collage1);
        System.out.println("collage = " + collage);
        if (collage1!=null)
            throw  new IllegalArgumentException("学院已存在");

        collage.setColId(idWorker.nextId() + "");
        collageMapper.insert(collage);
    }

    public void addMajor(Major major) {
        Example example = new Example(Major.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("majorname",major.getMajorname());
        criteria.andEqualTo("collageid",major.getCollageid());
        Major major1 = majorMapper.selectOneByExample(example);
        if (major1!=null)
            throw  new IllegalArgumentException("专业已存在");
        major.setId(idWorker.nextId() + "");
        majorMapper.insert(major);

}

    public void addClazz(Clazz clazz) {
        Example example = new Example(Clazz.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("majorname",clazz.getMajorname());
        criteria.andEqualTo("clazzName",clazz.getClazzName());
        Clazz clazz1 = clazzMapper.selectOneByExample(example);

        if (clazz1!=null)
            throw  new IllegalArgumentException("班级已存在");

        clazz.setClazzId(idWorker.nextId() + "");
        clazzMapper.insert(clazz);
    }
}
